package com.app.backend.common.exception;

import com.app.backend.common.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author xiaohzun
 */
@Slf4j
@RestControllerAdvice
public class RestCtrlExceptionHandler {

    @ExceptionHandler(AppException.class)
    @ResponseBody
    public BaseResponse handleAppException(AppException e) {
        String errorMsg = "app exception";
        if (e != null) {
            errorMsg = e.getMsg();
            log.error(e.toString(), e);
        }
        return BaseResponse.fail(errorMsg);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResponse handleException(Exception e) {

        String errorMsg = "Exception";
        if (e != null) {
            errorMsg = e.getMessage();
            log.error(e.toString(), e);
        }
        return BaseResponse.fail(errorMsg);
    }
}
